package com.bigdatan.b2c.controller.admin;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.bigdatan.b2c.conf.OnlineConfig;
import com.bigdatan.b2c.controller.AbstractController;
import com.bigdatan.b2c.controller.admin.vo.DetailDescInfoBO;
import com.bigdatan.b2c.controller.admin.vo.GoodsAdminVO;
import com.bigdatan.b2c.controller.glass.vo.SDKInfo;
import com.bigdatan.b2c.entity.Admin;
import com.bigdatan.b2c.entity.Goods;
import com.bigdatan.b2c.entity.GoodsPrice;
import com.bigdatan.b2c.service.IGoodsPriceService;
import com.bigdatan.b2c.service.IGoodsService;
import com.bigdatan.b2c.vo.QueryGoodsAdminVO;
import com.google.common.base.Function;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import constant.SystemCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import util.JsonResponse;
import util.PageResult;
import util.SessionUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

;

/**
 *  商品 管理模块 后台
 */
@Slf4j
@RestController
@RequestMapping("/admin/goods/goodsAdmin")
public class GoodsAdminController extends AbstractController {

    @Resource
    private IGoodsService goodsService;

    @Resource
    private IGoodsPriceService goodsPriceService;

    public static final Function<Goods, GoodsAdminVO> detailDescribe = goods -> {
        GoodsAdminVO vo = new GoodsAdminVO();
        try{
            BeanUtils.copyProperties(goods, vo, "detailDescribe");

            DetailDescInfoBO detailDescInfoBO = null;
            try {
                detailDescInfoBO = JSON.parseObject(goods.getDetailDescribe(), DetailDescInfoBO.class);
            } catch (Exception e) {
            }
            boolean notParseJson = detailDescInfoBO == null || detailDescInfoBO.getDetailDescribe() == null;
            if (notParseJson) {
                vo.setDetailDescribe(goods.getDetailDescribe());
            } else {
                BeanUtils.copyProperties(detailDescInfoBO, vo);
            }

            SDKInfo sdkInfo = null;
            try {
                sdkInfo = JSON.parseObject(goods.getSdkInfo(), SDKInfo.class);
            } catch (Exception e) {
            }
            boolean notParseSDKJson = sdkInfo == null || StringUtils.isBlank(sdkInfo.getPath());
            if (notParseSDKJson) {
            } else {
                StringBuilder sb = new StringBuilder();
                int size = sdkInfo.getDetailUrlList().size();
                for (int i = 0; i < size; i++) {
                    String str = sdkInfo.getDetailUrlList().get(i);
                    if (i >= size - 1){
                        sb.append(str);
                    } else {
                        sb.append(str + ",");
                    }
                }
                vo.setSdkDetail(sb.toString());
            }
        }catch (Exception e){
            log.error("goods detail Describe trans error!",e);
        }
        return vo;
    };

    /**
     * 添加商品
     */
    //@GetMapping("/addGoods")11
    @RequestMapping(method = {RequestMethod.POST, RequestMethod.GET},value = "/addGoods")
    public JsonResponse<Goods> addGoods(GoodsAdminVO goods, HttpServletRequest request) {
        JsonResponse<Goods> result = new JsonResponse<Goods>();

        String srcgoodspriceList = request.getParameter("goodspriceList");
        List<GoodsPrice> goodspriceList = (List<GoodsPrice>) JSONArray
                .parseArray(srcgoodspriceList, GoodsPrice.class);

        Admin admin = SessionUtil.getAdminUser(request);
        goods.setAdminId(admin.getAdminId());
        // SimpleDateFormat formatter = new
        // SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        goods.setCreateTime(new Date());
        goods.setUpdateTime(new Date());
        goods.setDelState((byte) 1);
        try {
            Goods goods1 = transGoodsVOToBO(goods);
            goodsService.insertSelective(goods1);
            //goods = goodsService.getOne(goods);// 为了获取保存后的id
            if (goodspriceList != null && goodspriceList.size() > 0) {
                for (int i = 0; i < goodspriceList.size(); i++) {
                    GoodsPrice temp = goodspriceList.get(i);
                    temp.setGoodsId(goods.getGoodsId());
                    temp.setAdminId(admin.getAdminId());
                    temp.setCreateTime(new Date());
                    temp.setUpdateTime(new Date());
                    temp.setState((byte) 1);
                    goodsPriceService.insertSelective(temp);
                }

            }

            result.setRes(SystemCode.SUCCESS);
        } catch (Exception e) {
            logError(request, "[admin:" + admin.getAdminName() + ",新增商品异常]", e);
            result.setRes(SystemCode.FAILURE);
        }
        return result;
    }

    private Goods transGoodsVOToBO(GoodsAdminVO goodsVO) {
        Goods goods1 = new Goods();
        BeanUtils.copyProperties(goodsVO, goods1);
        goods1.setDetailDescribe(getDetailDesc(goodsVO));
        String sdkDetail = goodsVO.getSdkDetail();

        List<String> strings = Splitter.on(",").omitEmptyStrings().trimResults().splitToList(sdkDetail);
        SDKInfo sdkInfo = new SDKInfo();
        String url = strings.get(0);
        // /usr/node/dist/pic/sdk/1502475283101/menu.txt
        int i = url.lastIndexOf("/");
        String path = url.substring(0, i);
        sdkInfo.setPath(OnlineConfig.UPLOAD_URL_PREFIX+path);
        sdkInfo.setDetailUrlList(strings);
        goods1.setSdkInfo(JSON.toJSONString(sdkInfo));
        return goods1;
    }

    public static void main(String[] args) {
        String url = "/usr/node/dist/pic/sdk/1502475283101/menu.txt";
        //
        int i = url.lastIndexOf("/");
        String path = url.substring(0, i);
        System.out.println(path);
    }

    private String getDetailDesc(GoodsAdminVO goods) {
        DetailDescInfoBO detailDescInfoBO = new DetailDescInfoBO();
        BeanUtils.copyProperties(goods, detailDescInfoBO);
        return JSON.toJSONString(detailDescInfoBO);
    }

    /**
     * 编辑商品
     */
    //@GetMapping("/editGoods")
    @RequestMapping(method = {RequestMethod.POST, RequestMethod.GET},value = "/editGoods")
    public JsonResponse<Goods> editGoods(GoodsAdminVO goods, HttpServletRequest request) {
        JsonResponse<Goods> result = new JsonResponse<Goods>();
        String srcgoodspriceList = request.getParameter("goodspriceList");
        List<GoodsPrice> goodspriceList = (List<GoodsPrice>) JSONArray
                .parseArray(srcgoodspriceList, GoodsPrice.class);
        if (goodspriceList == null) {
            goodspriceList = new ArrayList<GoodsPrice>();
        }
        Admin admin = SessionUtil.getAdminUser(request);
        goods.setAdminId(admin.getAdminId());
        goods.setUpdateTime(new Date());
        try {
            Goods goods1 = transGoodsVOToBO(goods);
            goodsService.updateByPrimaryKeySelective(goods1);
            // 删除旧数据
            List<GoodsPrice> hisgoodspriceList = goodsPriceService
                    .findAllNormalGoodsPriceByGoodsId(goods.getGoodsId());
            for (int y = 0; y < hisgoodspriceList.size(); y++) {
                GoodsPrice hist = hisgoodspriceList.get(y);
                boolean checkExist = false;
                for (int k = 0; k < goodspriceList.size(); k++) {
                    if (hist.getPriceId() == goodspriceList.get(k).getPriceId()) {
                        // 此次更新有数据
                        checkExist = true;
                    }
                }
                if (checkExist == true) {
                    // 历史数据当前有记录，不删除
                    continue;
                }
                hist.setState((byte) 2);// 删除
                hist.setAdminId(admin.getAdminId());
                hist.setUpdateTime(new Date());
                goodsPriceService.updateByPrimaryKeySelective(hist);
            }
            // 新增新数据
            for (int y = 0; y < goodspriceList.size(); y++) {
                GoodsPrice newt = goodspriceList.get(y);
                if (newt.getPriceId() == 0) {
                    // 新增的规格
                    newt.setPriceId(0);
                    newt.setGoodsId(goods.getGoodsId());
                    newt.setState((byte) 1);// 新增
                    newt.setAdminId(admin.getAdminId());
                    newt.setCreateTime(new Date());
                    newt.setUpdateTime(new Date());
                    goodsPriceService.insertSelective(newt);
                } else {
                    // 更新的规格，由于页面限制了不允许更新，因此不用处理
                    System.out.println("gengxin");
                }
            }
            result.setRes(SystemCode.SUCCESS);
        } catch (Exception e) {
            logError(request, "[admin:" + admin.getAdminName() + ",编辑商品异常]", e);
            result.setRes(SystemCode.FAILURE);
        }

        return result;
    }

    /**
     * 商品列表
     */

    //@GetMapping("/getGoodsByPage")
    @RequestMapping(method = {RequestMethod.POST, RequestMethod.GET},value = "/getGoodsByPage")
    public JsonResponse<PageResult<GoodsAdminVO>> getGoodsByPage(
            PageResult<Goods> page, QueryGoodsAdminVO goods, HttpServletRequest request) {
        JsonResponse<PageResult<GoodsAdminVO>> result = new JsonResponse<>();
        //goodsService.queryByPage(page, goods);
        goodsService.getPageByQueryGoodsAdminVO(page, goods);
        if (page.getTotal() != 0) {
            result.setRes(SystemCode.SUCCESS);
            result.setObj(transToVO(page));
        } else {
            result.setRes(SystemCode.FAILURE);
        }
        return result;
    }

    private PageResult<GoodsAdminVO> transToVO(PageResult<Goods> page) {

        List<GoodsAdminVO> goodsAdminVOs =  Lists.transform(page.getDataList(), detailDescribe);
        PageResult<GoodsAdminVO> result = new PageResult<>();
        result.setPageSize(page.getPageSize());
        result.setPageNo(page.getPageNo());
        result.setDataList(goodsAdminVOs);
        result.setPages(page.getPages());
        result.setTotal(page.getTotal());
        return result;
    }

    /**
     * 删除商品
     */
    //@GetMapping("/delGoodsById")
    @RequestMapping(method = {RequestMethod.POST, RequestMethod.GET},value = "/delGoodsById")
    public JsonResponse<Goods> delGoodsById(Goods goods,
                                            HttpServletRequest request) {
        JsonResponse<Goods> result = new JsonResponse<Goods>();
        Admin admin = SessionUtil.getAdminUser(request);
        goods.setAdminId(admin.getAdminId());
        goods.setDelState((byte) 2);
        goods.setUpdateTime(new Date());
        try {
            goodsService.updateByPrimaryKeySelective(goods);
            result.setRes(SystemCode.SUCCESS);
        } catch (Exception e) {
            logError(request, "[admin:" + admin.getAdminName() + ",删除商品异常]", e);
            result.setRes(SystemCode.FAILURE);
        }

        return result;
    }

    /**
     * 查找商品
     */
    //@GetMapping("/getGoodsById")
    @RequestMapping(method = {RequestMethod.POST, RequestMethod.GET},value = "/getGoodsById")
    public JsonResponse<GoodsAdminVO> getGoodsById(Integer goodsId,
                                            HttpServletRequest request) {
        JsonResponse<GoodsAdminVO> result = new JsonResponse<>();
        Goods goods = goodsService.selectByPrimaryKey(goodsId);
        if (goods != null) {
            result.setRes(SystemCode.SUCCESS);
            result.setObj(detailDescribe.apply(goods));
        }
        return result;
    }

}
